package form;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import com.bulenkov.darcula.DarculaLaf;

/**
 * Created by LLAP on 2017/12/7.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class Form extends JFrame {
    private JPanel mainPanel;
    private int maxHeight = 0;

    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }

    public void setMaxWidth(int maxWidth) {
        this.maxWidth = maxWidth;
    }

    private int maxWidth = 0;
    public Form(String title, int width, int height) {
        super(title);
        this.setSize(width, height);
        try {
            UIManager.setLookAndFeel(new DarculaLaf());
//            throw new IllegalArgumentException();
        } catch (Exception e) {
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
            }
            catch (Exception ex) {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                }
                catch (Exception exc) {
                    System.out.println("Substance Raven Graphite failed to initialize");
                }
            }
        }
        this.setLocationRelativeTo(getOwner());
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
        this.setContentPane(mainPanel);
    }

    public Form(int width, int height) {
        this("", width, height);
    }

    public Form(String title) {
        this(title, 0, 0);
        this.setSize(300, 300);
    }

    @Override
    public void pack() {
        super.pack();
        if (maxHeight > 0 && this.getHeight() > maxHeight) {
            this.setSize(this.getWidth(), maxHeight);
        }
        if (maxWidth > 0 && this.getWidth() > maxWidth) {
            this.setSize(maxWidth, this.getHeight());
        }
    }

    public Form() {
        this("");
    }

    public static void main(String[] args) {
        Form f = new Form(500, 500);
        f.add(new JLabel("111"));
    }
}
